﻿using System;
using System.Windows.Forms;
using Domain.Entity;
using QQZoneMarketingDao;
using Spider.Service;
using Timer = System.Timers.Timer;

namespace QQZoneMarketingService
{
    public class QQVisiteService
    {
        public static string CurrentQQSeed { get; set; }
        public static string StartToCatchEmailAddress(string seedUrl)
        {
            SpiderService.AddUrlToTodoQueue(new string[]
            {
                seedUrl
            });
            SpiderService.CommandSpiderToWork();
            AntService.CommandAntCaptainToWork();
            return "";
        }
        /// <summary>
        /// 启动QQ空间访问
        /// </summary>
        /// <param name="browser"></param>
        /// <returns></returns>
        public static string StartToQQZoneVisite(WebBrowser browser)
        {
            SpiderService.ReSetSpiderConfig();
            AntService.CommandAntCaptainToWork();
            SpiderService.CommandSpiderToWorkBySTA(browser);
            return "";
        }
        /// <summary>
        /// 自动定时添加待访问的QQ号码
        /// </summary>
        /// <param name="count"></param>
        public static void AutoAddWaitVisitedQQNumberForZone(int count)
        {
            var taskcount = SpiderMonitor.GetTodoTaskCount();
            if (taskcount == 0)
            {
                var qqsemails = QqEmailDao.GetHasNoVisiteQQZoneUserEmail(count);
                string[] qqs = new string[qqsemails.Length];
                for (int i = 0; i < qqsemails.Length; i++)
                {
                    qqs[i] =qqsemails[i].Replace("@qq.com", "");
                    QqEmailDao.SetQQZonHasVisited(qqsemails[i]);
                }
                SpiderService.AddContentToTodoQueue(qqs, "qqforzone");
            }
        }
        public static void ReSetAllQQAcountHasVisiteTimes0()
        {
            QQAcountDao.SetAllQQAcountVisiteTimes(0);
        }
        public static void ReSetAllQQAcountMaxVisiteTimes0(int times)
        {
            QQAcountDao.SetAllQQAcountMaxVisiteTimes(times);
        }
        public static int GetCurrentMaxVisiteTimes()
        {
            var first = CommonDao.QQAcountDao.GetFirstAcount();
            if (first != null)
            {
                return first.MaxVisiteTimes;
            }
            else
            {
                return 0;
            }
        }
        /// <summary>
        /// 定时更新QQ账号已经访问次数，方便自动每日定时访问
        /// </summary>
        public static void AutoUpdateQQAcountHasVisiteTimes()
        {
            Timer t = new Timer();
            t.Interval = 1000 * 60 * 30;//半小时执行一次
            t.Enabled = true;
            t.Elapsed += new System.Timers.ElapsedEventHandler(
            delegate
            {
                var qqAcounts = CommonDao.QQAcountDao.GetAllAcount();
                foreach (QQAcount qqAcount in qqAcounts)
                {
                    if (qqAcount.LastLogin != null)
                    {
                        if (DateTime.Now - qqAcount.LastLogin > new TimeSpan(0, 25, 0, 0))
                        {
                            QQAcountDao.SetQQAcountHasVisiteTimes(0, qqAcount.QQ);
                        }
                       
                    }
                    else
                    {
                        qqAcount.LastLogin = DateTime.Now;
                        QQAcountDao.SetQQAcountHasVisiteTimes(0, qqAcount.QQ);
                    }
                }
            });

        }

        public static void AddURLtoTaskQueen(string url)
        {
            SpiderService.AddUrlToTodoQueue(new string[] {url});
        }
    }
}
